/**
 * 店铺信息的model
 * @author: zxy
 * @update: 2017-03-09
 */
import {detail, save} from '../services/storeInfo';
import {parse} from 'qs';
import {message} from 'antd';

export default {

	namespace: 'storeInfo',

	state: {
		loading: false,
		btnLoading: false,
		item: {},
		mainMapList: [],
		envMapList: [],
		isAllDay: false, // 是否全天营业
		phone: '18305517359', // 店主手机号码
		dayilyBillTime: 1092600000, // 每日结算时间
		step: 0,
		modalVisible: false,
		modalLoading: false,
		modalBtnLoading: false,
	},

	subscriptions: {
		setup({dispatch, history}) {
			history.listen(location => {
				if (location.pathname === '/store-info') {
//					dispatch({
//						type: 'query',
//					});
				}
			});
		}
	},
	effects: {
		*detail({payload}, {call, put, select}) {
			const {id} = payload;
			const {data} = yield call(detail, id);
			if (data && data.success) {
					const res = data.data;
					
					yield put({
							type: 'updateAction',
							payload: {
									item: res,
							},
					});
			} else {
					message.error(data && data.message || '数据获取失败');
			}
		},
		*update({payload}, {call, put}) {
			yield put({type: 'showBtnLoading'});
			const {formData,} = payload;
			const {data} = yield call(save, formData);
			if (data && data.success) {
					message.success('保存成功');
					yield put({type: 'hideBtnLoading'});
			} else {
					yield put({type: 'hideBtnLoading'});
					message.error(data && data.message || '数据获取失败');
			}
		},
	},
	reducers: {
		showLoading(state) {
			return {...state, loading: true,};
		},
		hideLoading(state) {
			return {...state, loading: false,};
		},
		showModal(state) {
			return {...state, modalVisible: true,};
		},
		hideModal(state) {
			return {...state, modalVisible: false, step: 0, };
		},
		showBtnLoading(state) {
			return {...state, btnLoading: true,};
		},
		hideBtnLoading(state) {
			return {...state, btnLoading: false,};
		},
		updateAction(state, action) {
			return {...state, ...action.payload};
		},
	},
};
